package com.chencs.biz.category.controller;

import com.alibaba.fastjson.JSON;
import com.chencs.biz.category.service.CategoryService;
import com.chencs.vo.biz.category.CategoryItemModel;
import com.chencs.vo.biz.category.CategoryModel;
import com.chencs.vo.biz.category.TaoBaoCategoryModel;
import framework.model.ResponseOptUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * Created by fangzehua on 2017/7/4.
 */
@RestController
@RequestMapping("/category")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    @RequestMapping(value = "/getCategoryList", method = RequestMethod.POST, produces = "application/json; charset=UTF-8")
    public String getCategoryList(){
        List<CategoryModel> list = categoryService.getCategoryList();
        if(!list.isEmpty()){
            return JSON.toJSONString(list);
        }else{
            return ResponseOptUtil.commonMsgError("获取分类列表失败");
        }
    }

    @RequestMapping(value = "/insertCategory", method = RequestMethod.POST, produces = "application/json; charset=UTF-8")
    public String insertCategory(@RequestBody CategoryModel model){
        if( categoryService.insertCategory(model)){
            return ResponseOptUtil.commonSucess();
        }else{
            return ResponseOptUtil.commonMsgError("添加分类失败");
        }
    }
    /**
     * 分类详情
     * @param id
     * @return
     */
    @RequestMapping(value = "/findCategoryById", method = RequestMethod.POST, produces = "application/json; charset=UTF-8")
    public String findCategoryById(@RequestBody String id){
        CategoryModel categoryModel = categoryService.findCategoryById(id);
        if(categoryModel!=null){
            return JSON.toJSONString(categoryModel);
        }
        return ResponseOptUtil.commonMsgError("获取分类失敗");
    }

    /**
     * 修改分類
     * @param categoryModel
     */
    @RequestMapping(value = "/updateCategory", method = RequestMethod.POST, produces = "application/json; charset=UTF-8")
    public  String updateCategory(@RequestBody CategoryModel categoryModel){
            try {
                categoryService.updateCategory(categoryModel);
                return  ResponseOptUtil.commonSucess();
            }catch (Exception e){
                e.printStackTrace();
                return  ResponseOptUtil.commonMsgError("修改分類失敗");
            }
    }






    @RequestMapping(value = "/getTaoBaoCategoryList", method = RequestMethod.POST, produces = "application/json; charset=UTF-8")
    public String getTaoBaoCategoryList(){
        List<TaoBaoCategoryModel> list = categoryService.getTaoBaoCategoryList();
        if(!list.isEmpty()){
            return JSON.toJSONString(list);
        }else{
            return ResponseOptUtil.commonMsgError("获取淘寶分类列表失败");
        }
    }

    @RequestMapping(value = "/delTaoBaoCategoryById", method = RequestMethod.POST, produces = "application/json; charset=UTF-8")
    public String delTaoBaoCategoryById(@RequestBody String id){
        try {
            categoryService.delTaoBaoCategoryById(id);
            return ResponseOptUtil.commonSucess();
        }catch (Exception e){
            return ResponseOptUtil.commonMsgError("刪除淘寶分類失敗");
        }
    }



    @RequestMapping(value = "/insertCategoryItem", method = RequestMethod.POST, produces = "application/json; charset=UTF-8")
    public String insertCategoryItem(@RequestBody CategoryItemModel model){
        if( categoryService.insertCategoryItem(model)){
            return ResponseOptUtil.commonSucess();
        }else{
            return ResponseOptUtil.commonMsgError("添加分类失败");
        }
    }

    @RequestMapping(value = "/updateCategoryItem", method = RequestMethod.POST, produces = "application/json; charset=UTF-8")
    public String updateCategoryItem(@RequestBody CategoryItemModel model){
        if( categoryService.updateCategoryItem(model)){
            return ResponseOptUtil.commonSucess();
        }else{
            return ResponseOptUtil.commonMsgError("修改分类失败");
        }
    }


    @RequestMapping(value = "/getCategoryItemByCid", method = RequestMethod.POST, produces = "application/json; charset=UTF-8")
    public String getCategoryItemByCid(@RequestBody String cid){
        List<CategoryItemModel> list = categoryService.getCategoryItemByCid(cid);
        if(!list.isEmpty()){
            return JSON.toJSONString(list);
        }else{
            return ResponseOptUtil.commonMsgError("获取分类列表失败");
        }
    }


    @RequestMapping(value = "/findCategoryItemModelById", method = RequestMethod.POST, produces = "application/json; charset=UTF-8")
    public String findCategoryItemModelById(@RequestBody String id){
        CategoryItemModel model = categoryService.findCategoryItemModelById(id);
        if(model!=null){
            return JSON.toJSONString(model);
        }else{
            return ResponseOptUtil.commonMsgError("获取小分类失败");
        }
    }


}
